package com.pagepub.expo.action.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.expo.entity.CorpCatalog;
import com.pagepub.expo.entity.IdName;
import com.pagepub.expo.manager.AppSeqMng;
import com.pagepub.expo.manager.CorpCatalogMng;
@Controller("corpCatalogAction")
public class CorpCatalogAction  extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("corpCatalogMng")
	private CorpCatalogMng corpCatalogMng;
	
	@Autowired
	@Qualifier("appSeqMng")
	private AppSeqMng appSeqMng;

	
	public List<CorpCatalog> rows = new ArrayList<CorpCatalog>();
	
	private boolean success;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	public int  total;
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	public int start = 0;

	public long getNewId() {
		return newId;
	}

	public void setNewId(long newId) {
		this.newId = newId;
	}

	public int limit = 0;
	
	public long newId = 0;
	
	private List<IdName> ids;

	public List<IdName> getIds() {
		return ids;
	}

	public void setIds(List<IdName> catalogIds) {
		this.ids = catalogIds;
	}
	

	public CorpCatalogAction() {
		System.out.println("this is CorpCatalogAction");
	}

	public String execute() throws Exception {
		System.out.println("this is execute");
		return null;
	}

	public String all() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int start = Integer.valueOf(request.getParameter("start"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		String sortname = request.getParameter("sortname");
		String sortorder = request.getParameter("sortorder");
		int currentPage = start;
		int pageSize = limit;
		HashMap<String, String> param = new HashMap<String, String>();
		if (sortname != null && sortname.length() > 0) {
			param.put("sortname", sortname);
		}
		if (sortorder != null && sortorder.length() > 0) {
			param.put("sortorder", sortorder);
		}
		rows = corpCatalogMng.findByPage(currentPage, pageSize, param);
		total = corpCatalogMng.getCount(param);
		this.start = start;
		success = true;
		return SUCCESS;
	}
	
	public String allId() {
		rows = corpCatalogMng.findAll();
		success = true;
		return SUCCESS;
	}

	public List<CorpCatalog> getRows() {
		return rows;
	}

	public void setRows(List<CorpCatalog> rows) {
		this.rows = rows;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String delete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("delIds");
		ids = ids.substring(0, ids.length()-1);
		StringTokenizer st = new StringTokenizer(ids, ",", false);
		List<String> StringList = new ArrayList<String>();
		while (st.hasMoreElements()) {
			StringList.add(st.nextToken());
		}
		for (int i = 0; i < StringList.size(); i++) {
			corpCatalogMng.remove(new Long(StringList.get(i)));
		}
		success = true;
		return SUCCESS;
	}

	public String add() {
		corpCatalog.setAddTime(new Date());
		corpCatalogMng.add(corpCatalog);
		success = true;
		return SUCCESS;
	}
	
	
	public String newId(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String seqName = request.getParameter("seqName").trim();
		if(seqName==null || seqName.equals("")){
			newId=appSeqMng.getDefault();
		}else{
			newId = appSeqMng.getSequence(seqName);
		}
		success = true;
		return SUCCESS;
	}


	public String modify() {
		corpCatalog.setUpdateTime(new Date());
		corpCatalogMng.update(corpCatalog);
		success = true;
		return SUCCESS;
	}
	
	public String ids() {
		rows=null;
		ids = corpCatalogMng.findIdNames();
		success = true;
		return SUCCESS;
	}
	
	
	private CorpCatalog corpCatalog;

	public CorpCatalog getCorpCatalog() {
		return corpCatalog;
	}

	public void setCorpCatalog(CorpCatalog corpCatalog) {
		this.corpCatalog = corpCatalog;
	}
	
}
